package com.weetop.aklyscan.appConfig;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseUrl;
import com.youth.banner.loader.ImageLoader;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {

        //Glide 加载图片简单用法
        Glide.with(context)
                .load(path)
                .apply(new RequestOptions()
                        .apply(new RequestOptions().transforms(new CenterCrop()))//, new RoundedCorners(dp2px(5))
                        .placeholder(R.drawable.drawable_bg)
                        .fallback(R.drawable.drawable_bg)
                        .error(R.drawable.drawable_bg))
                .into(imageView);
     //   GlideUtil.load(context,imageView,(String)path);
    }

    public static Bitmap getBitmap(final String url) {
        URL imageURL = null;
        Bitmap bitmap = null;
        String bitmapUrl = "";
        bitmapUrl = url;
        if(bitmapUrl.contains("/public"))
            bitmapUrl = bitmapUrl.replace("/public","");
        Log.e("inuni", "URL = " + bitmapUrl);
        try {
            imageURL = new URL( bitmapUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) imageURL.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}